package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.domain.Department;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.IDepartmentService;
import cn.wolfcode.rbac.util.JsonResult;
import cn.wolfcode.rbac.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/department")
public class DepartmentController {
    
    @Autowired
    private IDepartmentService departmentService;

    // 提供一个方法处理查询所有部门数据请求，响应 HTML
    @RequestMapping("/list")
    //@RequiredPermission(name="部门列表")
    @RequiresPermissions(value={"department:list","部门列表"},logical= Logical.OR)
    // qo 要贴注解，@ModelAttribute 将数据添加到模型对象中，等价于 model.addAttribute("attributeName", qo);
    public String list(Model model,@ModelAttribute("qo") QueryObject qo){
        PageInfo result = departmentService.query(qo);
        model.addAttribute("result", result);
        return "department/list"; // /WEB-INF/views/department/list3.ftl
    }

 /*   @RequestMapping("/input")
    @RequiredPermission(name="部门编辑页面")
    public String input(Long id, Model model){
        if(id != null){ // 点击了修改按钮过来
            Department department = departmentService.get(id);
            model.addAttribute("department", department);
        }
        return "/WEB-INF/views/department/input.jsp"; // /WEB-INF/views/department/input.jsp
    }*/


    @RequestMapping("/saveOrUpdate")
    @ResponseBody   // 告诉 Spring MVC，我们需要转为 json格式返回给前端
   // @RequiredPermission(name="部门保存或修改")
    @RequiresPermissions(value={"department:saveOrUpdate","部门保存或修改"},logical= Logical.OR)
    public JsonResult saveOrUpdate(Department department){
        try {
            // int i = 1/0;
            if(department.getId() == null){ // 代表新增
                departmentService.save(department);
            }else { // 代表修改
                departmentService.update(department);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"操作失败！");
        }
        //return "redirect:/department/list.do";  // http://localhost/department/list.do
    }


    @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value={"department:delete","部门删除"},logical= Logical.OR)
    public JsonResult delete(Long id){
        // int i = 1/0;
        try {
            if (id != null) {
                departmentService.delete(id);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"操作失败！");
        }
        // return "redirect:/department/list.do";  // http://localhost/department/list.do
    }
}
